use 	"./output/cities_expandedpatents", clear

** npi X month-of-year FEs **
egen	idxm = group(gen_id f_m)

** census region X year FEs **
egen	regyr = group(f_yr region)

*======================*
* Treatment variables  *
*======================*

tab 	days_npis if f_myr==tm(1918m9)

gen		daysnpis = days_npis/30 
gen		lnnpis = ln(days_npis)
gen		longnpis = (days_npis>=90) if !mi(days_npis) // Natural break in data, about 61% have short

lab var daysnpis 	"(Days of NPIs)/30"
lab var lnnpis  	"Log Days of NPIS"
lab var longnpis 	"1[Days of NPIS>90]"

*=======================*
* ANNUAL  * 
*=======================*
foreach v of varlist daysnpis longnpis lnnpis {
	preserve
		local vartext : variable label `v'
		
		gen		myr_shifted = f_myr+4
		format  myr_shifted %tm

		gen		yr_shifted = year(dofm(myr_shifted))

		collapse (sum) pat_wtd_inv (mean) `v' pop_int, by(yr_shifted gen_id)
		
		drop if yr_shifted==1900 | yr_shifted==1930

		gen		T = yr_shifted-1919
		replace T = -7 if T<-7
		replace T = 7 if T>7 & !mi(T)

		char	T[omit] -1
		xi i.T, pref(_T)

		xtset gen_id yr_shifted
		ppmlhdfe pat_wtd_inv 1._TT_*#c.`v' if tin(1910, 1926), exp(pop_int) a(gen_id yr_shifted) cluster(gen_id)

		tempfile estud
		regsave using "`estud'", ci level(95) replace

		use "`estud'", clear

		gen		T_1 = substr(var,7,2)
		destring T_1, gen(T_2) i("#")

		gen		year = T_2 - 8
		drop if var=="_cons"
		drop	T_?

		set obs `=_N+1'
		replace year=-1 if _n==_N
		replace coef=0 if year==-1
		
		gen year_shifted = year+1919

		set scheme plotplainblind
		sort year
		scatter coef ci_* year_shifted, c(l l l) cmissing(y n n) msym(circle i i) legend(off) ///
			lcolor(black gray gray) lpattern(solid dash dash) lwidth(medthick medthin medthin) ///
			yline(0, lcolor(gs4) lp(solid)) xline(1918.5, lcolor(red)) ylabel(, nogrid) ///
			xlabel(1914(2)1924, nogrid) xlabel(1912 `""1912 or" "before""' 1926 `""1926 or" "later""', add) ///
			xtitle("Shifted Year") ytitle("Coef. on `vartext'") ///
			saving("$RES/event_study/annual_`v'", replace)
	
		graph export "$RES/event_study/annual_`v'.png", replace
		
		scatter coef ci_* year_shifted, c(l l l) cmissing(y n n) msym(circle i i) legend(off) ///
			lcolor(black gray gray) lpattern(solid dash dash) lwidth(medthick medthin medthin) ///
			yline(0, lcolor(gs4) lp(solid)) xline(1918.5, lcolor(red)) ylabel(, nogrid) ///
			xlabel(1914(2)1924, nogrid) xlabel(1912 `""1912 or" "before""' 1926 `""1926 or" "later""', add) ///
			xtitle("") ytitle("Coef. on `vartext'") ///
			saving("$RES/event_study/annual_`v'_noxtitle", replace)
			
	restore 
}
